#!/usr/bin/python
# -*- coding: utf-8 -*-

#-------------------------------------------------
#Create At: 2008-11-3 下午05:55:55 
#Create By: Fla.sam
#
#DESC: core_app models
#
#-------------------------------------------------

from django.contrib.auth.models import User, Group
from django.db import models
import utils

USER_TYPE_CHOISE = ( 
                     ( u'管理員', ( 
                                 ( u'AD_ADMIN', u'系统管理员' ), \
                                 ( u'AD_SMANAGER', u'超级管理员' ), \
                                 ( u'AD_MANAGER', u'管理员' ), \
                                 )
                     ), \
                     ( u'代理商', ( 
                                ( u'AD_AGENCY', u'省级广告代理商' ), \
                                )
                      ), \
                     ( u'AD_CUSTOMER', u'广告投放客户' ), \
                     ( u'AD_ENDUSER', u'广告終端客户' ), \
                     ( u'AD_ANONYMOUS', u'参观客户' )
                     ) 
  
BANK_CHOICES = ( 
                ( u'招商银行', u'招商银行' ), \
                ( u'工商银行', u'工商银行' ), \
                ( u'中国银行', u'中国银行' ), \
                ( u'建设银行', u'建设银行' ), \
                ( u'支付宝', u'支付宝' ), \
                )

# Create your models here.
class Place( models.Model ):
    
    place_name = models.CharField( verbose_name = u'地区名称', \
                                   max_length = 1024, \
                                   unique = True, \
                                   help_text = '设置地区名称' )
    place_parent = models.ForeignKey( 'self', \
                                      verbose_name = u'上级地区', \
                                      null = True, \
                                      related_name = 'child_set', \
                                      blank = True , \
                                      help_text = '设置该地区的上级地区' )
    
    def __unicode__( self ):
        p_list = self._recurse_for_parents( self )
        p_list.append( self.place_name )
        return self.get_separator().join( p_list )
    
    def _recurse_for_parents( self, cat_obj ):
        p_list = []
        if cat_obj.place_parent_id:
            p = cat_obj.place_parent
            p_list.append( p.place_name )
            more = self._recurse_for_parents( p )
            p_list.extend( more )
        if cat_obj == self and p_list:
            p_list.reverse()
        return p_list
    
    def _parents_repr( self ):
        p_list = self._recurse_for_parents( self )
        return self.get_separator().join( p_list )
    _parents_repr.short_description = "Tag parents"
    
    def get_separator( self ):
        return '-'
    
    class Meta:
        verbose_name = u'地理位置'
        verbose_name_plural = u'%s管理' % verbose_name
        
        permissions = ( ( 'can_view', u'允许查看' ), )
        
class User_CashTransferHistory( models.Model ):
    '''
            用户转账历史记录
    '''
    
    user = models.ForeignKey( User, \
                              verbose_name = '用户', \
                              help_text = '网站用户名' )
    request_transfer = models.BooleanField( verbose_name = '申请转账', \
                                            help_text = '用户是否提出转账申请', \
                                            blank = True )
    request_transfer_money = models.IntegerField( verbose_name = '用户申请转账金额', \
                                                  help_text = '用户所申请的转账金额', \
                                                  blank = True )
    request_transfer_date = models.DateField( verbose_name = '申请转账日', \
                                             help_text = '用户提交申请转账日期', \
                                             blank = True )
    transfer_date = models.DateField( verbose_name = '转账日期', \
                                      help_text = '转账日期', \
                                      blank = True )
    transfer_money = models.IntegerField( verbose_name = '金额', \
                                          help_text = '转账金额', \
                                          blank = True )
    transfer_result = models.BooleanField( verbose_name = '转账状态', \
                                           help_text = '给用户的汇款是否成功', \
                                           blank = True )
    transfer_result_desc = models.TextField( verbose_name = '转账状态说明', \
                                             help_text = '给用户的汇款结果说明', \
                                             blank = True )
    
    def __unicode__( self ):
        return u'%s 给用户%s 支付 %d, 是否支付成功:%s' % ( str( self.transfer_datetime ), self.user, self.transfer_money, str( self.transfer_result ) ) 
    
    class Meta:
        verbose_name = u'转账历史'
        verbose_name_plural = u'%s管理' % verbose_name
        
        permissions = ( ( 'can_view_by_manager', u'允许管理员可以查看' ), )
        
class UserProfile( models.Model ):
    '''
    Ad Project user model
    '''
    user = models.OneToOneField( User, \
                             unique = True, \
                             related_name = 'user', \
                             verbose_name = '用户名', \
                             help_text = '设置用户名' )
    
    user_create_by = models.ForeignKey( User, \
                                  null = True, \
                                  blank = False, \
                                  related_name = 'creator', \
                                  verbose_name = '管理员', \
                                  help_text = '创建该用户的管理员' )
    
    user_chinese_name = models.CharField( verbose_name = '姓名', \
                                          help_text = '联系人姓名', \
                                          max_length = 50 )
    user_phone = models.CharField( verbose_name = '电话', \
                                   help_text = '联系人电话', \
                                   max_length = 15 )
    user_desc = models.TextField( verbose_name = '用户说明', \
                                  help_text = '用户简要说明或备注' )
    user_unique_str = models.CharField( verbose_name = '唯一ID', \
                                        help_text = '用户的唯一ID, 不能更改,自动创建,用户用户计数', \
                                        max_length = 32, \
                                        default = utils.mk_key, \
                                        editable = False, \
                                        unique = True )
    user_u_point = models.IntegerField( verbose_name = '用户点数', \
                                        help_text = '用户累计点数', \
                                        blank = True, default = 0 )
    user_bank_name = models.CharField( max_length = 150, \
                                  choices = BANK_CHOICES, \
                                  verbose_name = '银行', \
                                  help_text = '用户银行名称', \
                                  blank = True )
    user_bank_acct = models.CharField( max_length = 150, \
                                  verbose_name = '账户', \
                                  help_text = '填写用户账号,支付宝用户请填写支付宝用户名', \
                                  blank = True )
    user_bank_acct_name = models.CharField( max_length = 100, \
                                       verbose_name = '银行账户所属用户名', \
                                       help_text = '填写银行账号所属账户名', \
                                       blank = True )
    user_identity_card = models.CharField( verbose_name = '用户身份证', \
                                      max_length = 18, \
                                      blank = True )
    
    class Meta:
        
        verbose_name = u'用户属性'
        verbose_name_plural = u'%s管理' % verbose_name
        
        permissions = ( ( 'can_view_by_manager', u'允许管理员查看' ), )
        
    def __unicode__( self ):
        return u'[ %s ], created by [ %s ]' % ( self.user, self.user_create_by )
    
    @property
    def is_ad_admin( self ):
        if len( self.user.groups.filter( name = 'AD_ADMIN' ) ) != 0:
            return True
        else:
            return False
    
    @property
    def is_ad_smanager( self ):
        if len( self.user.groups.filter( name = 'AD_SMANAGER' ) ) != 0:
            return True
        else:
            return False
    
    @property
    def is_ad_manager( self ):
        if len( self.user.groups.filter( name = 'AD_MANAGER' ) ) != 0:
            return True
        else:
            return False
    
    @property
    def is_ad_agency( self ):
        if len( self.user.groups.filter( name = 'AD_PROV_AGENCY' ) ) != 0:
            return True
        else:
            return False
    
    @property
    def is_ad_customer( self ):
        if len( self.user.groups.filter( name = 'AD_CUSTOMER' ) ) != 0:
            return True
        else:
            return False
    
    @property
    def is_ad_enduser( self ):
        if len( self.user.groups.filter( name = 'AD_ENDUSER' ) ) != 0:
            return True
        else:
            return False
    
    @property
    def is_ad_anonymous( self ):
        if len( self.user.groups.filter( name = 'AD_ANONYMOUS' ) ) != 0:
            return True
        else:
            return False
        
    def is_in_group( self, g ):
        if len( self.user.groups.filter( name = g ) ) != 0:
            return True
        else:
            return False
        
        
    
